module.exports = function(app){
    app.controller('merchantCtrl', function ($scope, $rootScope, $http, $interval, socketio) {
        
            $scope.initMerchant = function () {
                console.log("call merchant init()", $rootScope.merchant);
                // $scope.mockLogin(); // 仅测试时�
                // $interval(function () {
                //     console.log("wait 1s");
                $http({
                    method: 'GET',
                    url: 'merchant/currentUser'
                }).then(function successCallBack(res) {
                    console.log('callback merchant initMerchant() success', res);
                    // $scope.merchant = res.data.data;
                    $rootScope.merchant = res.data.data;
                    socketio.getSocket().emit('id', { id: $rootScope.merchant._id });
                    console.log('callback merchant initMerchant() success', $rootScope.merchant);
                }, function errCallback(res) {
                    console.log('callback merchant initMerchant() error', res);
                });
                // }, 1000, 1);    
            };
        
            $scope.logout = function () {
                console.log("call logout");
                $http({
                    method: 'GET',
                    url: 'api/merchant/login/logout'
                });
                window.location.href = 'index.html';
            };
        
            $scope.mockLogin = function () {
                console.log("call merchant mock login()");
                $http({
                    method: 'POST',
                    url: 'api/merchant/login',
                    data: { name: 'max', password: 'qwerdf' }
                }).then(function successCallBack(res) {
                    console.log('callback merchant mock login() success', res);
                }, function errCallback(res) {
                    console.log('callback merchant mock login() error', res);
                });
            }
        });
}
